home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / Sound / AudioReverberate.java.z / AudioReverberate.java
Encoding:
Java Source  |  2003-08-08  |  6.7 KB  |  165 lines

  1. /*
  2.  *    @(#)AudioReverberate.java 1.10 02/04/01 15:03:26
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import javax.media.j3d.*;
  41. import javax.vecmath.*;
  42. import java.util.Enumeration;
  43.  
  44. /*
  45.  * Pick the JavaSound reverb type that matches the input parameters
  46.  * as best as possible.
  47.  *
  48.  *  Hae Reverb Types     Size      Persistance        Delay
  49.  *  ================  -----------  ------------    -----------
  50.  *  1 None (dry)
  51.  *  2 "Closet"         very small  very short <.5   fast smooth
  52.  *  3 "Garage"         med. large    medium 1.0       medium
  53.  *  4 "Acoustic Lab"   med. small     short .5       med. fast
  54.  *  5 "Cavern"           large       long >2.0       very slow
  55.  *  6 "Dungeon"          medium    med. long 1.5     med. slow
  56.  *
  57.  *
  58.  * Order is NOT controllable, nor does it have a natural parallel.
  59.  * For this reason Order and Reflection are tied together as to
  60.  * affect 'Decay Speed'.  This speed paired with the size of the
  61.  * space implied by the Delay parameter determine the JavaSound
  62.  * Reverb type that is set:
  63.  *
  64.  *                      |  Short:               Long:
  65.  *                Speed |    Coeff <= 0.9         Coeff > 0.9
  66.  *  Size                |    Order <= 8           Order > 8
  67.  * ---------------------------------------------------------------
  68.  *  small (<100ms)      |    2 "Closet"           4 "Acoustic Lab"
  69.  *  medium  (<500ms)    |    3 "Garage"           6 "Dungeon"
  70.  *  large   (>500ms)    |    6 "Dungeon"          5 "Cavern"
  71.  */
  72. // User defined audio behavior class
  73. public class AudioReverberate extends Behavior {
  74.     WakeupOnElapsedTime  wt;
  75.     WakeupOnBehaviorPost wp;
  76.         PointSound           psound = new PointSound();
  77.         AuralAttributes           sScape = null;
  78.         static int           WAKEUP_SOUND = 0;
  79.         long                 dur;
  80.         long                 time;
  81.         boolean              firstTime = true;
  82.         String               fileName;
  83.         int                  lCount = 0;
  84.         int                  loopCount = 0;
  85.         
  86.     // Override Behavior's initialize method to setup wakeup criteria
  87.     public void initialize() {
  88.             MediaContainer sample  = new MediaContainer();
  89.             sample.setCacheEnable(true);
  90.             sample.setURLString(fileName);
  91.             psound.setSoundData(sample);
  92.             Point3f soundPos = new Point3f(-23.0f, 0.0f, 0.0f);
  93.             psound.setPosition(soundPos);
  94.             psound.setLoop(3);
  95.             firstTime = true;
  96.             System.out.println("Reverb Name  Size  Reflect  Order  Delay ");
  97.             System.out.println("-----------  ----  -------  -----  ----- ");
  98.         WakeupOnElapsedTime wp = new WakeupOnElapsedTime(5000);
  99.         wakeupOn(wp); 
  100.     }
  101.  
  102.     // Override Behavior's stimulus method to handle the event
  103.     public void processStimulus(Enumeration criteria) {
  104.             // time = System.currentTimeMillis();
  105.             if (firstTime)  {
  106.             wt = new WakeupOnElapsedTime(10000);
  107.                 firstTime = false;
  108.             }
  109.             else
  110.                 psound.setEnable(false) ; 
  111.  
  112.             if (++lCount > 6)
  113.                 lCount = 1;
  114.  
  115.             if (lCount == 1) {
  116.                 sScape.setReverbDelay(10.0f) ; 
  117.                 sScape.setReflectionCoefficient(0.5f) ; 
  118.                 sScape.setReverbOrder(5) ; 
  119.                 System.out.println("Closet        sm     0.5      5     10.0 ");
  120.             }
  121.             else if (lCount == 2) {
  122.                 sScape.setReverbDelay(10.0f) ; 
  123.                 sScape.setReflectionCoefficient(0.999f) ; 
  124.                 sScape.setReverbOrder(9) ; 
  125.                 System.out.println("Acoustic Lab  sm    0.999     9     10.0 ");
  126.             }
  127.             else if (lCount == 3) {
  128.                 sScape.setReverbDelay(200.0f) ; 
  129.                 sScape.setReflectionCoefficient(0.4f) ; 
  130.                 sScape.setReverbOrder(3) ;
  131.                 System.out.println("Garage        med    0.4      3    200.0 ");
  132.             }
  133.             else if (lCount == 4) {
  134.                 sScape.setReverbDelay(200.0f) ; 
  135.                 sScape.setReflectionCoefficient(0.99f) ; 
  136.                 sScape.setReverbOrder(10) ; 
  137.                 System.out.println("Dungeon       med   0.99     10    200.0 ");
  138.             }
  139.             else if (lCount == 5) {
  140.                 sScape.setReverbDelay(600.0f) ; 
  141.                 sScape.setReflectionCoefficient(0.33f) ; 
  142.                 sScape.setReverbOrder(7) ; 
  143.                 System.out.println("Dungeon       lrg   0.33      7    600.0 ");
  144.             }
  145.             else if (lCount == 6) {
  146.                 sScape.setReverbDelay(600.0f) ; 
  147.                 sScape.setReflectionCoefficient(1.0f) ; 
  148.                 sScape.setReverbOrder(20) ; 
  149.                 System.out.println("Cavern        lrg    1.0     20    600.0 ");
  150.             }
  151.             psound.setEnable(true); 
  152.         wakeupOn(wt);
  153.     }
  154.  
  155.         //
  156.     // Constructor for rotation behavior.  Parameter: front and back Sound nodes
  157.         //
  158.     public AudioReverberate(PointSound psound, String filename,
  159.                    AuralAttributes sscape) {
  160.             this.psound = psound;
  161.             this.fileName = filename;
  162.             this.sScape = sscape;
  163.     }
  164. }
  165.